home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Networking / JSaver / Source / JSaver.cp next >
Encoding:
Text File  |  1997-06-05  |  12.8 KB  |  521 lines  |  [TEXT/MPCC]

  1. /*
  2.  * JSaver.cp
  3.  *
  4.  * © 1997 Apple Computer, Inc.
  5.  *
  6.  * Written by Steve Zellers
  7.  */
  8.  
  9. #include "GraphicsModule_Types.h"
  10. #include <JManager.h>
  11. #include "NewAppletDialog.h"
  12.  
  13. /**
  14.  * Constants
  15.  */
  16.  
  17. #define URL_BUTTON_MESSAGE    8        /* message that brings up URL chooser dialog */
  18.  
  19. enum StartupStages {
  20.     eDoInitStage,            // we got our doInit msg
  21.     eCreateSessionStage,    // create a session now
  22.     eCreateContextStage,    // create an AWTContext
  23.     eLocateAppletStage,        // look for the applet
  24.     eLocateAppletNotDone,    // spin till we find it
  25.     eLocatorFailedStage,    // the locator failed to find the applet, or no applets were found.
  26.     eCreateViewerStage,        // create an applet viewer now
  27.     eRunningStage            // just give the VM time - everything is loaded
  28. };
  29.  
  30. /**
  31.  * Globals - note that we're specifically only going
  32.  * to excecute one java applet, so we have one
  33.  * Viewer, one Frame and one AWTContext.
  34.  */
  35.  
  36. static JMSessionRef theSession;
  37. static JMAWTContext theAWTContext;
  38. static JMAppletLocatorRef theLocatorRef;
  39. static JMAppletViewerRef theAppletViewer;
  40.  
  41. static Rect theViewerBounds;
  42. static Boolean theViewerVisible;
  43. static Boolean theFrameCreated;
  44. static JMFrameRef theJMFrame;
  45.  
  46. static RgnHandle theUpdateRgn = nil;
  47.  
  48. static StartupStages theStage = eDoInitStage;
  49.  
  50. static GMParamBlockPtr theParams;
  51. static Str255 theExceptionString;
  52.  
  53. static Boolean theFirstDrawFrameGotten = false;
  54.  
  55. static OSErr checkError(OSErr err)
  56. {
  57.     if (theExceptionString[0] > 0) {
  58.         if (err == noErr)
  59.             err = -1;
  60.         BlockMoveData(theExceptionString, theParams->errorMessage, theExceptionString[0] + 1);
  61.     }
  62.     return err;
  63. }
  64.  
  65. static void setMessage(StringPtr left, StringPtr right)
  66. {
  67.     static Rect lastRect = { 0, 0, 0, 0 };
  68.     
  69.     TextFont(geneva);
  70.     TextFace(normal);
  71.     TextSize(9);
  72.     if (! EmptyRect(&lastRect))
  73.         FillRect(&lastRect, (ConstPatternParam) &theParams->qdGlobalsCopy->qdBlack);
  74.     if (! (left || right))
  75.         SetRect(&lastRect, 0, 0, 0, 0);
  76.     else {
  77.         if (left == nil)
  78.             left = "\p";
  79.         if (right == nil)
  80.             right = "\p";
  81.         int totalWidth = (StringWidth("\p• ") * 2) + StringWidth(left) + StringWidth("\p ") + StringWidth(right);
  82.         TextMode(srcXor);
  83.         lastRect = theParams->monitors->monitorList[0].bounds;
  84.         lastRect.top = lastRect.bottom - 14;
  85.         lastRect.left = lastRect.left + ((lastRect.right - lastRect.left) / 2) - (totalWidth / 2);
  86.         lastRect.right = lastRect.left + totalWidth;
  87.         MoveTo(lastRect.left, lastRect.bottom - 4);
  88.         DrawString("\p• ");
  89.         DrawString(left);
  90.         DrawString(right);
  91.         DrawString("\p •");
  92.         TextMode(srcCopy);
  93.         PenNormal();
  94.     }
  95. }
  96.  
  97.  
  98. /**
  99.  * JMFrame support - these functions are used to 
  100.  * fill in a paramblock of function pointers.
  101.  */
  102. static void* _frameSetupPort(JMFrameRef frame)
  103. {
  104.     if (theViewerVisible) {
  105.         SetPort(theParams->qdGlobalsCopy->qdThePort);
  106.  
  107.         if (! theFirstDrawFrameGotten) {
  108.             theFirstDrawFrameGotten = true;
  109.             setMessage(nil, nil);
  110.         }
  111.  
  112.         SetOrigin(-theViewerBounds.left, -theViewerBounds.top);
  113.         OffsetRgn(theParams->qdGlobalsCopy->qdThePort->clipRgn, -theViewerBounds.left, -theViewerBounds.top);
  114.         
  115.         Rect r = theViewerBounds;
  116.         OffsetRect(&r, -r.left, -r.top);
  117.         ClipRect(&r);
  118.     }
  119.     return nil;
  120. }
  121.  
  122. static void _frameRestorePort(JMFrameRef frame, void* param)
  123. {
  124.     OffsetRgn(theParams->qdGlobalsCopy->qdThePort->clipRgn, theViewerBounds.left, theViewerBounds.top);
  125.     SetOrigin(0, 0);
  126. }
  127.  
  128. static Boolean _frameResizeRequest(JMFrameRef frame, Rect* desired)
  129. {
  130.     theViewerBounds.right = theViewerBounds.left + (desired->right - desired->left);
  131.     theViewerBounds.bottom = theViewerBounds.top + (desired->bottom - desired->top);
  132.     return true;
  133. }
  134.  
  135. static void _frameInvalRect(JMFrameRef frame, const Rect* r)
  136. {
  137.     RgnHandle rgn = NewRgn();
  138.     RectRgn(rgn, r);
  139.     UnionRgn(rgn, theUpdateRgn, theUpdateRgn);
  140.     DisposeRgn(rgn);
  141. }
  142.  
  143. static void _frameShowHide(JMFrameRef frame, Boolean showFrameRequested)
  144. {
  145.     if (theViewerVisible != showFrameRequested) {
  146.         theViewerVisible = showFrameRequested;
  147.         if (theViewerVisible) {
  148.             CopyRgn(theParams->qdGlobalsCopy->qdThePort->clipRgn, theUpdateRgn);
  149.             OffsetRgn(theUpdateRgn, -theViewerBounds.left, -theViewerBounds.top);
  150.             JMFrameUpdate(frame, theUpdateRgn);
  151.             SetEmptyRgn(theUpdateRgn);
  152.         }
  153.     }
  154. }
  155.  
  156. static void _frameSetTitle(JMFrameRef frame, Str255 title)
  157. {
  158. }
  159.  
  160. static void _frameCheckUpdate(JMFrameRef frame)
  161. {
  162.     if (! EmptyRgn(theUpdateRgn)) {
  163.         JMFrameUpdate(frame, theUpdateRgn);
  164.         SetEmptyRgn(theUpdateRgn);
  165.     }
  166. }
  167.  
  168. /*
  169.  * AppletViewer
  170.  */
  171.  
  172. static OSStatus _ctxRequestFrame(JMAWTContextRef context, JMFrameRef newFrame, JMFrameKind kind,
  173.                     UInt32 width, UInt32 height, Boolean resizeable, JMFrameCallbacks* callbacks)
  174. {
  175.     callbacks->fVersion = kJMVersion;
  176.     callbacks->fSetupPort = _frameSetupPort;
  177.     callbacks->fRestorePort = _frameRestorePort;
  178.     callbacks->fResizeRequest = _frameResizeRequest;
  179.     callbacks->fInvalRect = _frameInvalRect;
  180.     callbacks->fShowHide = _frameShowHide;
  181.     callbacks->fSetTitle = _frameSetTitle;
  182.     callbacks->fCheckUpdate = _frameCheckUpdate;
  183.     
  184.     if (kind == eModelessWindowFrame) {
  185.         theJMFrame = newFrame;
  186.         theFrameCreated = true;
  187.     }
  188.  
  189.     return noErr;
  190. }
  191.  
  192. static OSStatus _ctxReleaseFrame(JMAWTContextRef context, JMFrameRef oldFrame)
  193. {
  194.     if (theJMFrame == oldFrame) {
  195.         theJMFrame = nil;
  196.         theFrameCreated = false;
  197.     }
  198.     return noErr;
  199. }
  200.  
  201. static SInt16 _ctxUniqueMenuID(JMAWTContextRef context, Boolean isSubmenu)
  202. {
  203.     return 0;    // we don't allow for menus
  204. }
  205.  
  206. static int strlen(const char* p)
  207. {
  208.     int len = 0;
  209.     while (*p++)
  210.         len++;
  211.     return len;
  212. }
  213.  
  214. static void _ctxExceptionOccurred(JMAWTContextRef context, const char* exceptionName, const char* exceptionMsg, const char* stackTrace)
  215. {
  216.     Handle hExceptionString = GetResource('TEXT', exceptionMsg == nil? 130 : 131);
  217.     if (hExceptionString == nil) {
  218.         theExceptionString[0] = 1;
  219.         theExceptionString[1] = 0;
  220.     } else {
  221.         Munger(hExceptionString, 0, "^0", 2, exceptionName, strlen(exceptionName));
  222.         if (exceptionMsg)
  223.             Munger(hExceptionString, 0, "^1", 2, exceptionMsg, strlen(exceptionMsg));
  224.         int maxLen = GetHandleSize(hExceptionString);
  225.         if (maxLen >= 255)
  226.             maxLen = 254;
  227.         theExceptionString[0] = maxLen;
  228.         BlockMoveData(*hExceptionString, &theExceptionString[1], maxLen);
  229.     }
  230. }
  231.  
  232. /*
  233.  * After Dark
  234.  */
  235.  
  236. OSErr DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params)
  237. {
  238.     // set up some globals - our actual initialization is staged accross
  239.     // subsequent doDrawFrames.
  240.     theParams = params;
  241.     theExceptionString[0] = 0;
  242.     theStage = eCreateSessionStage;
  243.     theUpdateRgn = NewRgn();
  244.     return noErr;
  245. }
  246.  
  247. OSErr DoClose(Handle storage,RgnHandle blankRgn,GMParamBlockPtr params)
  248. {
  249.     OSStatus result = noErr;
  250.     
  251.     if (theSession != nil) {
  252.         if (theAppletViewer != nil) {
  253.             if (result == noErr) {
  254.                 result = JMSuspendApplet(theAppletViewer);
  255.                 if (result == noErr) {
  256.                     if (theJMFrame) {
  257.                         result = JMFrameGoAway(theJMFrame);
  258.                         if (result == noErr) {
  259.                             int i;
  260.                             for (i = 0;  i < 6 && theFrameCreated && theJMFrame;  i++)
  261.                                 JMIdle(theSession, kDefaultJMTime);
  262.                         }
  263.                     }
  264.                 }
  265.                 result = JMDisposeAppletViewer(theAppletViewer);
  266.                 theAppletViewer = nil;
  267.             }
  268.         }
  269.         
  270.         if (theAWTContext != nil) {
  271.             result = JMDisposeAWTContext(theAWTContext);
  272.             theAWTContext = nil;
  273.         }
  274.         
  275.         if (theLocatorRef != nil) {
  276.             result = JMDisposeAppletLocator(theLocatorRef);
  277.             theLocatorRef = nil;
  278.         }    
  279.         result = checkError(JMCloseSession(theSession));
  280.     }
  281.     
  282.     if (theUpdateRgn) {    
  283.         DisposeRgn(theUpdateRgn);
  284.         theUpdateRgn = nil;
  285.     }
  286.  
  287.     return result;
  288. }
  289.  
  290. OSErr DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  291. {
  292.     OSErr result = noErr;
  293.  
  294.     // Simply blanks out the area that we are going to draw in.
  295.     FillRgn(blankRgn, (ConstPatternParam) ¶ms->qdGlobalsCopy->qdBlack);
  296.     
  297.     return checkError(result);
  298. }
  299.  
  300. // the control value is between 0 (.5s) and 100 (6s)
  301. #define getTime(xx) \
  302.     ((xx + 1) * 500)
  303.  
  304. static OSErr createSession()
  305. {
  306.     JMSessionCallbacks callbacks;
  307.     JMSecurityOptions security;
  308.  
  309.     // fill in session callbacks
  310.     callbacks.fVersion = kJMVersion;
  311.     callbacks.fStandardOutput = nil;
  312.     callbacks.fStandardError = nil;
  313.     callbacks.fStandardIn = nil;
  314.  
  315.     // fill in security story - we don't support running
  316.     // within a firewall.
  317.     security.fVersion = kJMVersion;
  318.     security.fVerifyMode = eCheckRemoteCode;
  319.     security.fUseHttpProxy = false;
  320.     security.fHttpProxy[0] = 0;
  321.     security.fHttpProxyPort = 0;
  322.     security.fUseFTPProxy = false;
  323.     security.fFTPProxy[0] = 0;
  324.     security.fFTPProxyPort = 0;
  325.     security.fUseFirewallProxy = false;
  326.     security.fFirewallProxy[0] = 0;
  327.     security.fFirewallProxyPort = 0;
  328.     security.fNetworkAccess = eUnrestrictedAccess;
  329.     security.fFileSystemAccess = eAllFSAccess;
  330.     security.fRestrictClassAccess = true;
  331.     security.fRestrictClassDefine = true;
  332.  
  333.     setMessage("\pInitializing Java", nil);
  334.  
  335.     /*
  336.      * create a session
  337.      */
  338.     return JMOpenSession(&theSession, &security, &callbacks, 0L);
  339. }
  340.  
  341. static OSErr createContext()
  342. {
  343.     JMAWTContextCallbacks callbacks;
  344.     callbacks.fVersion = kJMVersion;
  345.     callbacks.fRequestFrame = _ctxRequestFrame;
  346.     callbacks.fReleaseFrame = _ctxReleaseFrame;
  347.     callbacks.fUniqueMenuID = _ctxUniqueMenuID;
  348.     callbacks.fExceptionOccurred = _ctxExceptionOccurred;
  349.     
  350.     setMessage("\pCreating an AWTContext", nil);
  351.  
  352.     OSErr result = JMNewAWTContext(&theAWTContext, theSession, &callbacks, 0L);
  353.     if (result == noErr) {
  354.         result = JMResumeAWTContext(theAWTContext);
  355.     }
  356.     return result;
  357. }
  358.  
  359. static void _locatorCompleted(JMAppletLocatorRef ref, JMLocatorErrors status)
  360. {
  361.     StringPtr pMsg = nil;
  362.     
  363.     switch (status) {
  364.         case eLocatorNoErr:
  365.             break;
  366.         case eHostNotFound:
  367.             pMsg = "\pThe host could not be found";
  368.             break;
  369.         case eFileNotFound:
  370.             pMsg = "\pThe file could not be found";
  371.             break;
  372.         case eLocatorTimeout:
  373.             pMsg = "\pThe host could not be reached";
  374.             break;
  375.         case eLocatorKilled:
  376.             pMsg = "\pThe locator was killed";
  377.             break;
  378.     }
  379.     
  380.     if (pMsg == nil)
  381.         theStage = eCreateViewerStage;
  382.     else {
  383.         setMessage(pMsg, nil);
  384.         theStage = eLocatorFailedStage;
  385.     }
  386. }
  387.  
  388. static OSErr createAppletLocator()
  389. {
  390.     Str255 urlText;
  391.     Str255 labelText;
  392.     getSelectedURL(urlText, labelText);
  393.     urlText[urlText[0] + 1] = 0;
  394.     setMessage("\pFetching the html page for ", labelText);
  395.  
  396.     JMAppletLocatorCallbacks locatorCallbacks;
  397.     locatorCallbacks.fVersion = kJMVersion;
  398.     locatorCallbacks.fCompleted = _locatorCompleted;
  399.     
  400.     return JMNewAppletLocator(&theLocatorRef, theSession, &locatorCallbacks, (const char*) &urlText[1], nil, 0L);
  401. }
  402.  
  403. static OSErr createViewer()
  404. {
  405.     if (theLocatorRef == nil)
  406.         return paramErr;
  407.  
  408.     if (theAWTContext == nil)
  409.         return paramErr;
  410.  
  411.     OSErr result = noErr;
  412.     UInt32 count;
  413.     result = JMCountApplets(theLocatorRef, &count);
  414.     if (result == noErr) {
  415.         if (count == 0)
  416.             _ctxExceptionOccurred(theAWTContext, "No applet tags were found on that page.", nil, nil);
  417.         else {
  418.             UInt32 width, height;
  419.             result = JMGetAppletDimensions(theLocatorRef, 0, &width, &height);
  420.     
  421.             if (result == noErr) {
  422.                 Rect* totalRect = &theParams->monitors->monitorList[0].bounds;
  423.                 JMAppletViewerCallbacks callbacks;
  424.                 
  425.                 theViewerVisible = false;
  426.                 theFrameCreated = false;
  427.                 
  428.                 theViewerBounds.left = totalRect->left + ((totalRect->right - totalRect->left) / 2) - (width / 2);
  429.                 theViewerBounds.top = totalRect->top + ((totalRect->bottom - totalRect->top) / 2) - (height / 2);
  430.                 theViewerBounds.right = theViewerBounds.left + width;
  431.                 theViewerBounds.bottom = theViewerBounds.top + height;
  432.                 
  433.                 callbacks.fVersion = kJMVersion;
  434.                 callbacks.fShowDocument = nil;
  435.                 callbacks.fSetStatusMsg = nil;
  436.  
  437.                 setMessage("\pCreating the applet viewer and loading classes.", "\p");
  438.                 
  439.                 result = JMNewAppletViewer(&theAppletViewer, theAWTContext, theLocatorRef, 0, &callbacks, 0L);
  440.                 
  441.                 if (result == noErr)
  442.                     result = JMReloadApplet(theAppletViewer);
  443.             }
  444.         }
  445.     }
  446.     
  447.     (void) JMDisposeAppletLocator(theLocatorRef);
  448.     theLocatorRef = nil;
  449.     
  450.     return result;
  451. }
  452.  
  453. static OSErr idleSession()
  454. {
  455.     OSErr result = noErr;
  456.     if (theSession != nil)
  457.         result = JMIdle(theSession, kDefaultJMTime);
  458.     return result;
  459. }
  460.  
  461. OSErr DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  462. {
  463.     OSErr result = noErr;
  464.     
  465.     if (theStage == eRunningStage || theStage == eLocateAppletNotDone) {
  466.         if (theStage == eRunningStage) {
  467.             if (theJMFrame && theViewerVisible)
  468.                 _frameCheckUpdate(theJMFrame);
  469.         }
  470.         result = idleSession();
  471.     } else {
  472.         switch (theStage) {
  473.             case eCreateSessionStage:
  474.                 result = createSession();
  475.                 if (result == noErr)
  476.                     theStage = eCreateContextStage;
  477.                 break;
  478.                 
  479.             case eCreateContextStage:
  480.                 result = createContext();
  481.                 if (result == noErr)
  482.                     theStage = eLocateAppletStage;
  483.                 break;
  484.  
  485.             case eLocateAppletStage:
  486.                 result = createAppletLocator();
  487.                 if (result == noErr)
  488.                     theStage = eLocateAppletNotDone;
  489.                 break;
  490.             
  491.             case eCreateViewerStage:
  492.                 result = createViewer();
  493.                 if (result == noErr)
  494.                     theStage = eRunningStage;
  495.                 break;
  496.             
  497.             case eLocatorFailedStage:
  498.                 result = fnfErr;
  499.                 break;
  500.         }
  501.     }
  502.     
  503.     return checkError(result);
  504. }
  505.  
  506. OSErr DoSetUp(RgnHandle blankRgn ,short message, GMParamBlockPtr params)
  507. {
  508.     Handle hText;
  509.     
  510.     switch (message) {
  511.         case URL_BUTTON_MESSAGE:
  512.             newAppletDialog();
  513.             break;
  514.  
  515.         default:
  516.             SysBeep(1);
  517.             break;
  518.         }
  519.     return noErr;
  520. }
  521.